#include <pthread.h>

#include <iostream>
#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>
#include <unistd.h>

#define LOG_CONF_FILE "socketpool_test_log.xml"

#define MY_LOGGER_NAME "shadow.sscf.socketpool.test"

#include "SocketPool.h"

using std::cout;
using std::endl;

#define THREAD_NUM 50

log4cxx::LoggerPtr g_logger = log4cxx::Logger::getLogger(MY_LOGGER_NAME);

void * test_socket_pool_thread_routine(void * arg) {
		SocketPool * sp = (SocketPool *)arg;
		while (true) {
				SSocket * s = sp->acquire_socket();
				string buf("dddd");
				s->send_all(buf.c_str(), buf.size(), 2);
				sp->release_socket(s);
				sleep(1);
		}
		return NULL;
}

void * test_socket_pool_thread_routine_empty(void * arg) {
		while (true) {
				sleep(1);
		}
		return NULL;
}

int test_socket_pool() {
		SocketPool sp;
		sp.init(10, "127.0.0.1", 5566, 3);
		pthread_t t[THREAD_NUM];
		for (int i = 0; i < THREAD_NUM; i++) {
				pthread_create(&t[i], NULL, test_socket_pool_thread_routine, &sp);
		}
		for (int i = 0; i < THREAD_NUM; i++) {
				pthread_join(t[i], NULL);
		}
		return 1;
}

int test_a_socket() {
		while (true) {
				SSocket s;
				int res = s.connect("10.10.11.226", 3000, 2);
				if (res > 0) {
						cout << "connect ok, fd:" << s.get_fd() << endl;
						s.close();
				}
				else {
						cout << "connect error, res:" << res << endl;
				}
		}
}


int main(int argc, char * argv[]) {
	log4cxx::xml::DOMConfigurator::configure(LOG_CONF_FILE);
//	test_a_socket();
	test_socket_pool();
	cout << ".......oops" << endl;
	return 0;
}
